/*-----------------------------------------------------------------------------
-通用开发平台:基本组件的始类
-作者：张金军
-所有人：张金军
-版权：版权所有，张金军，2005-2015，保留所有权利
-----------------------------------------------------------------------------*/
 /** 
  * fieldType:text、int、number、money、card、email、utl、date
  **/
Yacon.widget.Input = function(conText)
{
	this.fieldType   = "text";
	this.maxLength   = -1;
	this.minLength   = -1;
	this.maxValue    = -1;
	this.minValue    = -1;
	this.pointLength = -1;
	
	this.isNull      = true;
	this.ImeMode     = "";
	
	this.fieldLabel = "" ;
	
	conText = conText ||{};
	
	Yacon.apply(this,conText);
	
	this.create();
}
Yacon.widget.Input.Extend(Yacon.widget.WinControl,{
	
	attachElement:function(attachElement)
	{
		
		if (!attachElement)
			return;
		
		this.attach = this.setAttach(attachElement);
		if (!this.element && !this.elementID)
		{
			this.createTemplate();
			addElement(this.element);
		}
		this.element.Control = this;
		this.elementType     = "input";
			
		this.type        	= this.element.type;
		this.fieldName      = this.attach.getAttribute("fieldName").trim();
		this.fieldType      = this.attach.getAttribute("fieldType");
		this.fieldNull      = Yacon.isTrue(this.attach.getAttribute("fieldNull"));
		this.maxLength   	= Yacon.getInt(this.attach.getAttribute("maxLength"));
		this.minLength   	= Yacon.getInt(this.attach.getAttribute("minLength"));
		this.maxValue   	= this.attach.getAttribute("maxValue");
		this.minValue   	= this.attach.getAttribute("minValue");
		
		this.imeMode        = Yacon.isTrue(this.attach.getAttribute("imeMode"));
		
		this.fieldLabel = this.attach.getAttribute("fieldLabel"); 
				
		var attachPost   = Yacon.getAbsPosition(this.attach);
		
			
		this.element.style.left = attachPost[0]; 
		this.element.style.top  = attachPost[1];  
		this.element.style.width = attachPost[2]; 
		this.element.style.height = attachPost[3]-2;
		this.element.style.visibility = "visible";  
		this.element.style.position = "absolute"; 
		
		
		this.setFieldType();
		if (Yacon.isTrue(this.attach.getAttribute("fieldDisabled")))
			this.setDisabled(true);	
		
		if(this.attach.getAttribute("dataSet"))		
			this.setPriFieldValue(getDatasetByID(this.attach.getAttribute("dataSet")).getFieldValue(this.fieldName));

		this.element.focus();
	},
	
	create:function()
	{

		if (this.attach)
			this.attachElement(this.attach);
		
		if (!this.element)
			this.element   = document.getElementById(this.elementID);
		else
			this.elementID = this.element.getAttribute("id");
		
	
		if (!this.element)
			return;	
		
		this.initialization();
		
		if (this.element.tagName.isEqual("textarea"))
		{
			this.addEvents("keypress", Yacon.widget.Input.events.onkeypress);
			this.addEvents("keydown", Yacon.widget.Input.events.onkeydown);
			this.addEvents("keyup", Yacon.widget.Input.events.onchange);
			this.addEvents("change", Yacon.widget.Input.events.onchange);
		}else{
			if (this.type.isEqual("text") ||this.type == "password")
			{
				
				this.addEvents("keypress", Yacon.widget.Input.events.onkeypress);
				this.addEvents("keydown", Yacon.widget.Input.events.onkeydown);
				this.addEvents("change", Yacon.widget.Input.events.onchange);
				this.addEvents("keyup", Yacon.widget.Input.events.onkeyup);
				this.addEvents("click", Yacon.widget.Input.events.onclick);
				this.addEvents("beforepaste", Yacon.widget.Input.events.onbeforepaste);
			}
		}
	},
	
	initialization:function()
	{
		this.element.Control = this;
		this.elementType     = "input";
		
		this.type        	= this.element.type;
		this.fieldName      = this.element.getAttribute("fieldName");
		this.fieldType      = this.element.getAttribute("fieldType");
		this.fieldNull      = Yacon.isTrue(this.element.getAttribute("ISNull"));
		this.maxLength   	= Yacon.getInt(this.element.getAttribute("maxLen"));
		this.minLength   	= Yacon.getInt(this.element.getAttribute("minLength"));
		this.maxValue   	= this.element.getAttribute("maxValue");
		this.minValue   	= this.element.getAttribute("minValue");
		this.imeMode        = Yacon.isTrue(this.element.getAttribute("imeMode"));
		
		this.fieldLabel = this.element.getAttribute("fieldLabel"); 	
			
		this.setDataSet();
		this.setFieldType();
				
		if (Yacon.isTrue(this.element.getAttribute("fieldDisabled")))
			this.setDisabled(true);	
		else
			this.setDisabled();
	},
	addEvents:function(eventName,eventHander)
	{
		 if(window.attachEvent)
			 this.element.attachEvent("on" + eventName, eventHander );
		  else
			 this.element.addEventListener(eventName, eventHander, false);
	},
	setFieldType:function()
	{
		switch(this.fieldType)
		{
			case Yacon.widget.FIELD_INT:
			case Yacon.widget.FIELD_NUMBER:
			case Yacon.widget.FIELD_MONEY:
			case Yacon.widget.FIELD_CARD:
			case Yacon.widget.FIELD_EMAIL:
			case Yacon.widget.FIELD_URL:
			case Yacon.widget.DATE:
			case Yacon.widget.IP:
			{
				this.element.style.imeMode="disabled";
				this.imeMode = true;
				break;
			}
			case Yacon.widget.FIELD_TEXT:
			case Yacon.widget.FIELD_TEXTAREA:
			{
				if (this.imeMode)
					this.element.style.imeMode="disabled";
				else
					this.element.style.imeMode="active";				
			}
			default:
			{
				
			}
		}
			
	},
	setDataSet:function(dataSet)
	{
		if (!dataSet)
		{
			if (window.dataSet)
				this.dataSet = window.dataSet;
			else
			   	this.dataSet   =  getDatasetByID(this.element.getAttribute("dataSet"));
			
			if (this.dataSet)
			{
				this.dataSet.regElement(this);
				addElement(this.element);
				this.setPriFieldValue(this.dataSet.getFieldValue(this.fieldName));
			}	
			
		}else
		{
			this.dataSet = dataSet;
		}
	},
	setDisabled:function(disabled)
	{
		
		if (!Yacon.isEmpty(disabled))
		{
			this.element.disabled = disabled;
		}else
		{
			if (this.dataSet && this.dataSet.getRecord())
			{	
				if (this.dataSet.getRecord().getState().isEqual("insert"))
				{
					if(window.isIE8 || window.isIE9){
						this.element.disabled = Yacon.isTrue(this.element.insertDisabled);
					}else{
						this.element.disabled = Yacon.isTrue(this.element.getAttribute("insertDisabled"));
					}
				}else if (this.dataSet.getRecord().getState().isEqual("edit"))
				{
					if(window.isIE8 || window.isIE9){
						this.element.disabled = Yacon.isTrue(this.element.editDisabled);
					}else{
						this.element.disabled = Yacon.isTrue(this.element.getAttribute("editDisabled"));
					}
				}else
				{
					this.element.disabled = true;
				}
			}
		}
	},
	setPriFieldValue:function(fieldvalue,fieldText)
	{
		this.element.value = fieldvalue		
	},
	setFieldValue:function(fieldvalue)
	{
		if (!Yacon.isDefined(fieldvalue))
		{
		
			switch(this.fieldType)
			{
				case Yacon.widget.FIELD_INT:
				{
					 if ((Yacon.getInt(this.maxValue) > 0) && (Yacon.getInt(fieldvalue) >Yacon.getInt(this.maxValue)))
					 {		 
						 fieldvalue = Yacon.getInt(this.maxValue);
					 }
					 if ((Yacon.getInt(this.minValue) > 0) && (Yacon.getInt(fieldvalue) < Yacon.getInt(this.minValue)))
					 {		 
						 fieldvalue = Yacon.getInt(this.minValue);
					 }
					 if (this.maxLength > 0 && fieldvalue.length > this.maxLength)
					 {		
						 while((fieldvalue+"").length > this.maxLength)
						 {
							 fieldvalue = Yacon.getInt(fieldvalue) -1;
						}							
					}
					
					break;				
				}
				case Yacon.widget.FIELD_NUMBER:
				{
					if ((Yacon.getFloat(this.maxValue) > 0) && (Yacon.getFloat(fieldvalue) > Yacon.getFloat(this.maxValue)))
					 {		 
						fieldvalue = Yacon.getFloat(this.maxValue);
					 }
					if ((Yacon.getFloat(this.minValue) > 0) && (Yacon.getFloat(fieldvalue)< Yacon.getFloat(this.minValue)))
					 {		 
						fieldvalue = Yacon.getFloat(this.minValue);
					 }
					 fieldvalue = fieldvalue+"";
					 var prcValue="";
					 var dicValue="";
					 if (fieldvalue.contains("."))
					 {
						 var vals = fieldvalue.split(".");
						 prcValue = vals[0];
						 dicValue = vals[1];
					 }
					 
					 if (this.maxLength > 0 && prcValue.length > this.maxLength)
					 {		
						 while((prcValue+"").length > this.maxLength)
						 {
							 prcValue = Yacon.getFloat(prcValue) -1;
						 }							
					}
					
					if (this.minLength > 0 && dicValue.length > this.minLength)
					{		
						dicValue = dicValue.substring(0, this.minLength);						
					}
					if (fieldvalue.contains("."))
						 fieldvalue =prcValue+"."+dicValue;
				
					break;				
				}
				case Yacon.widget.FIELD_TEXT:
				case Yacon.widget.FIELD_TEXTAREA:
				{
					
					if (this.maxLength > 0 && fieldvalue.getCLength() > this.maxLength)
					{		
						fieldvalue = fieldvalue.subCStr(0, this.maxLength);
					}
					break;
				}
			}
			
			this.element.value = fieldvalue;
			if (this.dataSet)
				this.dataSet.setFieldValue(this.fieldName,this.element.value);
		
			if (this.attach && this.attach.getAttribute("dataSet"))
			{
				getDatasetByID(this.attach.getAttribute("dataSet")).setFieldValue(this.fieldName,this.element.value);
			}
		}
	},
	getFieldValue:function()
	{
		if (this.dataSet)
			return this.dataSet.getFieldValue(this.fieldName);
		else if (this.element)
			return this.element.value;
		else
			return "";
	 },	
	 ifCheckNull:function()
	 {
		if (!this.fieldNull)
		{
			if (this.element.tagName.isEqual("textarea"))
			{
				var value = this.element.value.trim();
				if (value.length == 0) {					
					addNullElement(this.element);
				}
	
			}else
			{
				if (this.type.isEqual("text")|| this.type.isEqual("password"))
				{
					var value = this.element.value.trim();
					if (value.length == 0) {					
						addNullElement(this.element);
					}
		
				} else if (this.type.isEqual("checkbox")&& !this.element.checked) 
				{
					addNullElement(this.element);
				}
			}
		}
		
		if (this.minLength>0)
		{
			var value = this.element.value.trim();
			if (value.length < this.minLength) 
				addMinLenElement(this.element);
		}
			
	},
	createTemplate:function()
	{
		if (!document.all("template_input"))
		{	
			this.element = document.createElement("input");
			this.element.className = "active_editor";
			this.element.setAttribute("id","template_input");
			this.element.setAttribute("dataSet","template_DataSet");
			Yacon.addEvents(this.element,"blur",Yacon.widget.Input.events.onBlur); 
			document.body.appendChild(this.element);
		}else
		{
			this.element =  document.all("template_input");
		}
		this.elementID = "template_input";
	},
	free:function()
	{
		if (this.dataSet)
			this.dataSet.unregElement(this.element);		
	}
	
});
	
Yacon.widget.Input.events= {	
		onclick:function()
		{
			var element   = event.srcElement;		
			if (Yacon.isfireuserEvent(element.Control.elementID+"_onclick"))
			{
				if (!Yacon.fireUserEvent(element.Control.elementID+"_onclick",[this]))
					return;
			}
		},
		onbeforepaste:function()
		{
			var element   = event.srcElement;
			var value     = clipboardData.getData('text');
			
			switch(element.Control.fieldType)
			{
			    case Yacon.widget.FIELD_INT:
				{
					if (!/^\d+$/.test(value))
						clipboardData.setData('text',"");
					else{
						 
						 if ((Yacon.getInt(element.Control.maxValue) > 0) && (Yacon.getInt(value) >Yacon.getInt(element.Control.maxValue)))
						 {		 
							 value = Yacon.getInt(element.Control.maxValue);
							 clipboardData.setData('text',value+"");
						 }
						 if (element.Control.maxLength > 0 && value.length > element.Control.maxLength)
						 {		
							 while((value+"").length > element.Control.maxLength)
							 {
								 value = Yacon.getInt(value) -1;
							 }	
							 clipboardData.setData('text',value+"");
						}					 
					}
					break;				
				}  
				case Yacon.widget.FIELD_NUMBER:
				{
					if (!/^\d+\.\d+$/.test(value)&&!/^\d+$/.test(value))
						clipboardData.setData('text',"");
					else{
						if ((Yacon.getFloat(element.Control.maxValue) > 0) && (Yacon.getFloat(value) > Yacon.getFloat(element.Control.maxValue)))
						 {		 
							 value = Yacon.getFloat(element.Control.maxValue);
							 clipboardData.setData('text',value+"");
						 }
						 value = value+"";
						 var dicValue="";
						 if (value.contains("."))
						 {
							 var vals = value.split(".");
							 value = vals[0];
							 dicValue = vals[1];
						 }
						 
						 if (element.Control.maxLength > 0 && value.length > element.Control.maxLength)
						 {		
							 while((value+"").length > element.Control.maxLength)
							 {
								 value = Yacon.getFloat(value) -1;
							 }							
						}
						
						if (element.Control.minLength > 0 && dicValue.length > element.Control.minLength)
						{		
							dicValue = dicValue.substring(0, element.Control.minLength);						
						}
						
						 if (!Yacon.isEmpty(dicValue))
							value =value+"."+dicValue;
						
						clipboardData.setData('text',value+"");
						 
					}
					 break;				
				}
				case Yacon.widget.FIELD_TEXT:
				case Yacon.widget.FIELD_TEXTAREA:
				{
					if (element.Control.imeMode)
					{
						try{
							window.clipboardData ; 
						}catch(e){
							return ;
						}
						value = value.replace(/[^\x00-\xFF]/g,'');
						if (element.Control.maxLength > 0 && value.length > element.Control.maxLength)
						{		
							 value = value.substring(0, element.Control.maxLength);								 
						}
						clipboardData.setData('text',value);
							
					}else
					{
						value = clipboardData.getData('text');						
						if (element.Control.maxLength > 0 && value.getCLength() > element.Control.maxLength)
						{		
							 value = value.subCStr(0, element.Control.maxLength);
							 clipboardData.setData('text',value);
						}
							
					}
					break;				
				}
			}			
	 	},
	 	onchange:function()
	 	{
		 
	 		var element   = event.srcElement;
			var value     = element.value;
			
			switch(element.Control.fieldType)
			{
				case Yacon.widget.FIELD_INT:
				{
					 if ((Yacon.getInt(element.Control.maxValue) > 0) && (Yacon.getInt(value) >Yacon.getInt(element.Control.maxValue)))
					 {		 
						 value = Yacon.getInt(element.Control.maxValue);
					 }
					 if (element.Control.maxLength > 0 && value.length > element.Control.maxLength)
					 {		
						 while((value+"").length > element.Control.maxLength)
						 {
							 value = Yacon.getInt(value) -1;
						}							
					}
						
					break;				
				}
				case Yacon.widget.FIELD_NUMBER:
				{
					if ((Yacon.getFloat(element.Control.maxValue) > 0) && (Yacon.getFloat(value) > Yacon.getFloat(element.Control.maxValue)))
					 {		 
						 value = Yacon.getFloat(element.Control.maxValue);
					 }
					 value = value+"";
					 var dicValue="";
					 if (value.contains("."))
					 {
						 var vals = value.split(".");
						 value = vals[0];
						 dicValue = vals[1];
					 }
					 
					 if (element.Control.maxLength > 0 && value.length > element.Control.maxLength)
					 {		
						 while((value+"").length > element.Control.maxLength)
						 {
							 value = Yacon.getFloat(value) -1;
						 }							
					}
					
					if (element.Control.minLength > 0 && dicValue.length > element.Control.minLength)
					{		
						dicValue = dicValue.substring(0, element.Control.minLength);						
					}
					
					 if (!Yacon.isEmpty(dicValue))
						value =value+"."+dicValue;
					
					break;				
				}
				case Yacon.widget.FIELD_TEXT:
				case Yacon.widget.FIELD_TEXTAREA:			
				{
					if (!element.Control.imeMode)
					{
						if (element.Control.maxLength > 0 && value.length > element.Control.maxLength)
						{		
							 value = value.substring(0, element.Control.maxLength);
						}
					}else
					{
						if (element.Control.maxLength > 0 && value.getCLength() > element.Control.maxLength)
						{		
							 value = value.subCStr(0, element.Control.maxLength);
						}
					}
					
					break;				
				}				
			}
			
			element.Control.setFieldValue(value);
			/*外部调用函数*/
			if (Yacon.isfireuserEvent(element.fieldName+"_onchange"))
			{
				Yacon.fireUserEvent(element.fieldName+"_onchange",[element]);
			}
	 	},
	 	onkeypress:function()
	 	{
			
	 		 var key       = event.charCode||event.keyCode;
			 var element   = event.srcElement;
			 var selection = Yacon.getSelection(element);
			 var value 	   = element.value;
			 var cStr	   = String.fromCharCode(key);
			 var vreturn   = true;
			 
			 value= "" + value.substring(0,selection.start)
			 	+ cStr + value.substring(selection.end,value.length);
			
			 switch(element.Control.fieldType)
			 {
			 	
			 	case Yacon.widget.FIELD_INT:
				{
					if ((key < 48 && key!=13 && key!=3 ) || key > 57 )
					{
						 vreturn  = false;               
		                 break;
					}else
					{
						
						if ((Yacon.getInt(element.Control.maxValue) > 0) && (Yacon.getInt(value) >Yacon.getInt(element.Control.maxValue)))
						{
							 vreturn  = false;                   
				             break;
						 }
						 if (element.Control.maxLength > 0 && value.length > element.Control.maxLength)
						 {
							 vreturn  = false;                   
				             break;
						 }
					}
					break;					
				}case Yacon.widget.FIELD_NUMBER:
				{
							
					var numArray=event.srcElement.value.split(".");
					if (numArray.length > 1 && key==46 )
					{
						 vreturn  = false;               
		                 break;
					}
					if (event.srcElement.negative)
					{
						if (key > 57 || (key !=46 && key !=45 && key < 48))
						{
							 vreturn  = false;               
			                 break;
						}
					}
					else
					{
						if (key > 57 || (key !=46 && key < 48))
						{
							 vreturn  = false;               
			                 break;
						}
					}					
					 if ((Yacon.getFloat(element.Control.maxValue) > 0) && (Yacon.getFloat(value) > Yacon.getFloat(element.Control.maxValue)))
					 {
						 vreturn  = false;                   
			             break;
					 }
					 value = value+"";
					 var dicValue="";
					 if (value.contains("."))
					 {
						 var vals = value.split(".");
						 value = vals[0];
						 dicValue = vals[1];
					 }
					 
					 if (element.Control.maxLength > 0 && value.length > element.Control.maxLength)
					 {		
						 vreturn  = false;                   
			             break;						
					}
					
					if (element.Control.minLength > 0 && dicValue.length > element.Control.minLength)
					{		
						vreturn  = false;                   
			             break;					
					}
					
					break;					
				}
				case Yacon.widget.FIELD_TEXT:
				case Yacon.widget.FIELD_TEXTAREA:			
				{
					
					if (element.Control.maxLength > 0 && value.getCLength() > element.Control.maxLength)
					{	
						vreturn  = false;                   
				        break;
					}
					
					break;					
				}
			}
			 if (!vreturn)
			 {
			 	 if(event.preventDefault)
	            	 event.preventDefault();
	         
			 	 event.returnValue=false;
			 }
             
	 	},
	 
	 	onkeydown:function()
	 	{
			 var key     = event.charCode||event.keyCode;
			 var element = event.srcElement;
			 var value 	 = element.value;	 
			
			 switch(key)
			 {
			 	/**
			 	 * 回车键
			 	 */	
			 	case 13:
			 	{
					if(element.getAttribute("nextid"))
					{
						if (document.all(element.getAttribute("nextid")).tagName.toUpperCase()== "SPAN")
							document.all(element.getAttribute("nextid")).fireEvent("onclick")
						if (document.all(element.getAttribute("nextid")).tagName.toUpperCase()== "INPUT")
							document.all(element.getAttribute("nextid")).focus();
					} else {
						 window.event.keyCode = 9;
					}
					break;
			 	}
			 	/**up*/
			  	case 38:
			  	{
			  
			  		if (element.Control.fieldType.isEqual(Yacon.widget.FIELD_INT))
			  		{
				 		 value   = !isNaN(parseInt(value))?parseInt(value):"";
				 		 var vch = true;
				 		 
				 		if ((Yacon.getInt(element.Control.maxValue) > 0) && (Yacon.getInt(value) >Yacon.getInt(element.Control.maxValue)))
						{		 
							 value = element.Control.maxValue;
							 vch  = false;
						 }
						 if (element.Control.maxLength > 0 && (value+"").length > element.Control.maxLength)
						 {		
							 vch  = false;
						 }
				 		if (vch)
				 		{
					 		if(value == "")
					 			value=0;
					 		
					 		element.Control.setFieldValue(value+1);
				 		}
			  		}
	               break;
			  	}/**down*/      
			 	case 40:
			 	{
			 		if (element.Control.fieldType.isEqual(Yacon.widget.FIELD_INT))
			  		{
				 		 value   = !isNaN(parseInt(value))?parseInt(value):"";
				 		 if(value == "")
					 		value=0;
					 	  if (value > 0)
					 	  {
					 		  element.Control.setFieldValue(value - 1);
					 	  }
				 	}
			 	  break;
			 	}
			 }
	 },
	 onkeyup:function()
	 {
		 var key     = event.charCode||event.keyCode;
		
		 if (key==9 || key==13|| key==37|| key==39)
			return;
		
		 var element   = event.srcElement;
		 var value     = element.value;
		 if (window.isIE)
		 {
			 var  testRange =document.selection.createRange();  
			 testRange.setEndPoint("StartToStart",element.createTextRange())  
			 var pointLen = testRange.text.length;
			 
			 element.Control.setFieldValue(value);
			 
			 var  textRange  =element.createTextRange();  
			 textRange.moveStart('character',pointLen);  
			 textRange.collapse(true);  
			 textRange.select(); 
		 }
	 }
	 ,
	 onBlur:function()
	 {
		event.srcElement.style.visibility = "hidden"; 
	 }
};

